/**
 * Created by Administrator on 2016/7/17 0017.
 */
var contextPath = "/";
$(function () {
    contextPath += window.location.pathname.split("/")[1];
    $("#name").focus(function () {
        //TODO:获取焦点提示
        $("#name_mes").html("姓名不能超过5个汉字");
    });
    $("#name").blur(function () {
        //TODO：失去焦点检查信息
        checkName();
    });
    function checkName() {
        var name = $("#name").val();

        var reg = /^[\u4e00-\u9fa5]{2,5}$/;
        if(!reg.test(name+"")){
            $("#name_ok").html("");
            $("#name_mes").html("<img src = '"+contextPath+"/media/img/no.png'/><span class='no'>请输入正确的姓名</span>");
            return false;
        }else {
            $("#name_mes").html("");
            $("#name_ok").html("<img src = '"+contextPath+"/media/img/ok.png'/>");
            return true;
        }
    }
    $("#phone").focus(function () {
        //TODO:获取焦点提示
        $("#phone_mes").html("11位手机号");
    });
    $("#phone").blur(function () {
        //TODO：失去焦点检查信息
        checkPhone();
    });
    function checkPhone() {
        var flag;
        var phone = $("#phone").val();
        var reg = /^1[34578]{1}[0-9]{9}$/;
        if(!reg.test(phone)){
            $("#phone_ok").html("");
            $("#phone_mes").html("<img src = '"+contextPath+"/media/img/no.png'/><span class='no'>请输入正确的手机号</span>");
            flag=false;
        }else {
            var url = contextPath+"/reg/checkPhone.wm";
            var data = {};
            data.shoujihao=phone;
            $.ajaxSetup({
                async : false
            });
            $.post(url,data,function(result){
                if(result.msg!="yes"){
                    $("#phone_ok").html("");
                    $("#phone_mes").html("<img src = '"+contextPath+"/media/img/no.png'/><span class='no'>手机号已被注册</span>");
                    flag=false;
                }else{
                    $("#phone_mes").html("");
                    $("#phone_ok").html("<img src = '"+contextPath+"/media/img/ok.png'/>");
                    flag=true;
                }
            },"json");
        }
        return flag;
    }
    $("#passwd").focus(function () {
        //TODO:获取焦点提示
        $("#passwd_mes").html("<table style='display:inline;'> <tr><td>安全强度：</td><td class = 'ceil' id='low'>低</td><td class = 'ceil' id='mid'>中</td><td class = 'ceil' id='high'>高</td></tr> </table>");
        streng();
    });
    $('#passwd').bind('input propertychange', function() {
        streng();
    });
    function streng() {
        var passwd = $("#passwd").val();
        var flag1 = 0;
        var flag2 = 0;
        var flag3 = 0;
        var reg1 = /^[a-z|A-Z]$/;
        var reg2 = /^[0-9]{1}$/;
        for(var i = 0;i <passwd.length;i++){
            if(reg1.test(passwd.charAt(i))){
                flag1=1;
            }else if(reg2.test(passwd.charAt(i))){
                flag2=1
            }else {
                flag3=1;
            }
        }
        var flag = flag1+flag2+flag3;
        if(flag==1||passwd.length<5){
            $("#low").css({"backgroundColor":"red","color":"black"});
            $("#mid").css({"backgroundColor":"#DDDDDD","color":"black"});
            $("#high").css({"backgroundColor":"#DDDDDD","color":"black"});
        }
        if((flag==2&&passwd.length>5)||(flag==1&&passwd.length>9)){
            $("#low").css({"backgroundColor":"#FF9D04","color":"black"});
            $("#mid").css({"backgroundColor":"#FF9D04","color":"black"});
            $("#high").css({"backgroundColor":"#DDDDDD","color":"black"});
        }else if(flag==3&&passwd.length>5){
            $("#low").css({"backgroundColor":"#90CB11","color":"black"});
            $("#mid").css({"backgroundColor":"#90CB11","color":"black"});
            $("#high").css({"backgroundColor":"#90CB11","color":"black"});
        }
    }
    $("#passwd").blur(function () {
        //TODO：失去焦点检查信息
        checkPasswd();
    });
    function checkPasswd() {
        var passwd = $("#passwd").val();
        var reg = /^[^\s]+$/;
        if(!reg.test(passwd)){
            $("#passwd_ok").html("");
            $("#passwd_mes").html("<img src = '"+contextPath+"/media/img/no.png'/><span class='no'>密码长度为6-20位,不允许空格</span>");
            return false;
        }else {
            $("#passwd_ok").html("<img src ='"+contextPath+"/media/img/ok.png'/>");
            streng();
            return true;
        }
    }
    $("#confPasswd").focus(function () {
        //TODO:获取焦点提示
        $("#confPasswd_mes").html("再次输入密码");
    });
    $("#confPasswd").blur(function () {
        //TODO：失去焦点检查信息
        checkConfPasswd();
    });
    function checkConfPasswd() {
        var passwd = $("#passwd").val();
        var confPasswd = $("#confPasswd").val();
        if(passwd!=confPasswd||passwd==""){
            $("#confPasswd_ok").html("");
            $("#confPasswd_mes").html("<img src = '"+contextPath+"/media/img/no.png'/><span class='no'>两次输入密码不一致</span>");
            return false;
        }else{
            $("#confPasswd_mes").html("");
            $("#confPasswd_ok").html("<img src = '"+contextPath+"/media/img/ok.png'/>");
            return true;
        }
    }
    $("#regSub").click(function () {
        var flag = true;
        if(!(checkName()&&checkPasswd()&&checkPhone()&&checkConfPasswd())){
            flag = false;
            alert("请填写正确的注册信息！")
        }
        if(flag){
            $("#regForm").submit();
            alert("恭喜你注册成功！")
        }
    });
})
